package com.zline.foodraise.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.zline.foodraise.constant.Constant;
import com.zline.foodraise.ui.activity.PayFailActivity_;
import com.zline.foodraise.ui.activity.PaySuccessActivity_;
import com.zline.foodraise.util.LUtil;

/**
 * Created by heshantao on 15/8/12.
 */
public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
    private static final String LOG_TAG = WXPayEntryActivity.class.getSimpleName();
    private IWXAPI api;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(this, Constant.WX_APP_ID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq baseReq) {

    }

    @Override
    public void onResp(BaseResp baseResp) {
        String result="";
        int isSuccess=0;
        if (baseResp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            switch (baseResp.errCode) {
                case BaseResp.ErrCode.ERR_OK://支付成功
                    PaySuccessActivity_.intent(this).start();
                    LUtil.i(LOG_TAG,"_____1_______");
                    break;
                case BaseResp.ErrCode.ERR_COMM://支付异常
                    result="支付异常";
                    PayFailActivity_.intent(this).erroMsg(result).start();
                    LUtil.i(LOG_TAG,"_____2_______");
                    break;
                case BaseResp.ErrCode.ERR_USER_CANCEL://用户取消
                    result="用户取消";
                    PayFailActivity_.intent(this).erroMsg(result).start();
                    LUtil.i(LOG_TAG,"_____3_______");
                    break;
                case BaseResp.ErrCode.ERR_AUTH_DENIED://认证否决
                    result="认证否决";
                    PayFailActivity_.intent(this).erroMsg(result).start();
                    LUtil.i(LOG_TAG,"_____4_______");
                    break;
                case BaseResp.ErrCode.ERR_SENT_FAILED://发送失败
                    result="发送失败";
                    PayFailActivity_.intent(this).erroMsg(result).start();
                    LUtil.i(LOG_TAG,"_____5_______");
                    break;
                case BaseResp.ErrCode.ERR_UNSUPPORT://微信版本不支持
                    result="微信版本不支持";
                    PayFailActivity_.intent(this).erroMsg(result).start();
                    break;
                default:
                    break;
            }
            finish();
        }

    }
}
